// pages/caseSrc/myCaseSrc/myCaseSrc.js
import request from '../../../utils/config.js';
import formatDate from '../../../utils/util.js';
// import cityList from '../../../utils/vendor/cityList.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: wx.getSystemInfoSync().windowHeight - 40,
    indicatorDots: true,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    titleShow: true,
    current: 0,
    // caseSrcList: [],
    // pageNum: 1,
    // totalPage: '',
    orderFlag: true,
    endFlag: false,
    orderCaseList: [],
    endCaseList: []
  },

  getApplyCaseList: function () {
    let _this = this;
    let headerInfo = {
      pageNum: '',
      pageSize: "",
      sessionId: app.globalData.sessionId,
    }, bizInfo = {};
    // if (pageNum == 1) {
      if (this.data.orderFlag) {
        this.setData({
          orderCaseList: []
        });
      } 
      if (this.data.endFlag) {
        this.setData({
          endCaseList: []
        });
      }
    // }
    // if (flag) {
    //   wx.showLoading({ title: '', mask: true })
    // }
    request.requestLogin_api("queryCaseOrder", headerInfo, bizInfo, (obj) => {
      wx.hideLoading()
      console.log('obj=',obj)
      if (obj.data.isSuccess == 'T') {
        // if (JSON.parse(obj.data.body).caseOrderList.length == 0) {
        //   wx.showToast({ title: '暂无数据', icon: 'none', duration: 1000 });
        // }
        // let pageNum = obj.data.pageNum;
        // let totalPage = obj.data.totalPage;

        let tempArr = JSON.parse(obj.data.body).caseOrderList;
        console.log('tempArr', tempArr)

        let tempList;
        //已承接
        if (_this.data.orderFlag) {
            tempList = _this.data.orderCaseList;//orderCaseList 
        }
        //已结束
        if (_this.data.endFlag) {
            tempList = _this.data.endCaseList;//orderCaseList
        }
        tempArr.forEach(function (e) {
          // 金额、日期过滤
          e.caseAmt = formatDate.filterFormatAmt(e.caseAmt * 1 / 10000);
          e.status = formatDate.investStatusFilter(e.status);
          e.caseTypeId = formatDate.filterCaseTypeId(e.caseTypeId)
          // 城市过滤
          let address = [];
          e.caseLbsInfos.forEach(function (y) {
            let str = '';
            if (y.province) {
              str = y.province
            }
            if (y.city) {
              str += ' ' + y.city
            }
            if (y.country) {
              str += ' ' + y.country
            }
            address.push(str)
          })
          address = address.toString();
          if (!address) {
            address = '暂无'
          }
          e.address = address;
          // 判断承接  结束 赋值
          if (_this.data.orderFlag) {
            if (e.orderStatus == '1') {
              tempList.push(e)
            } 
          }
          if (_this.data.endFlag) {
            if (e.orderStatus == '2' || e.orderStatus == '9') {
              tempList.push(e)
            }
          }
        });
        if (_this.data.orderFlag) {
          _this.setData({
            orderCaseList: tempList,
            // pageNum: pageNum,
            // totalPage: totalPage
          });
        }
        if (_this.data.endFlag) {
          _this.setData({
            endCaseList: tempList,
            // pageNum: pageNum,
            // totalPage: totalPage
          });
        }
      }
    }, (obj) => {
      wx.hideLoading();
      console.log("获取案源列表失败==", obj)
      wx.showModal({
        title: "加载失败",
        content: '请尝试下拉界面重新加载',
        confirmText: '确定',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
    })
  },
  
  swiperChange: function (e) {
    // console.log(e)
    if (e.detail.source == "touch") {
      this.setData({ current: e.detail.current })
    }
  },
  title1: function () {
    this.setData({
      titleShow: true,
      current: 0,
      // pageNum: 1,
      orderFlag: true,
      endFlag: false
    })
    this.getApplyCaseList()
  },
  title2: function () {
    this.setData({
      titleShow: false,
      current: 1,
      // pageNum: 1,
      orderFlag: false,
      endFlag: true
    })
    this.getApplyCaseList()
  },

  goCaseDetail: function(e) {
    // console.log(e.currentTarget.dataset.id)
    let caseId = e.currentTarget.dataset.id;
    wx.navigateTo({
      // url: '../myCaseDetail/myCaseDetail?caseId=' + caseId,
      url: '../../caseDescribe/caseDescribe?caseId=' + caseId,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getApplyCaseList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  upper: function (e) {
    console.log("顶部=", e)
  },
  lower: function (e) {
    console.log("底部=", e)
    let pageNum = this.data.pageNum;
    let totalPage = this.data.totalPage;
    console.log('pageNum', pageNum)
    console.log('totalPage', totalPage)
    if (pageNum < totalPage) {
      this.getApplyCaseList(pageNum * 1 + 1, false)
    } else {

    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})